Imports System.Data

Partial Class WebForms_CIssue
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage

        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Select Case PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim dao As New DAOMain.VW_POLICY_CLIENT_DTL_DAO
            Dim vo As New DAOMain.VW_POLICY_CLIENT_DTL_VO
            Dim lfunc As New PolyFunc.ASPFunc
            Dim lookup As New LookUp.FillLookUp

            Call InitVars()

            Policy_td.InnerText = ViewState("PolicyNo")
            provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()

            Call LoadIssue()

            vo.POLICY_SEQ_CD = ViewState("Seq_CD")
            Dim dt As DataTable = dao.Search(vo)

            'lfunc.BindDropDown(Name_cmb, dt, "NAME", "POLICY_CLIENT_CD")
            lookup.FillDropDown(IssueStatus_cmb, lookup.eLookUpType.other, "ISSUE_STATUS")

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))

            sec = Nothing
            lookup = Nothing
            dao = Nothing
            vo = Nothing
        End If
    End Sub

    Private Sub LoadIssue()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_ISSUE_DTL_DAO
        Dim vo As New DAOMain.POLICY_ISSUE_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.POLICY_ISSUE_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Remarks_txt.Text = .Item("REMARKS")
                IssueDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_ISSUE_DT"))
                IssueStatus_cmb.SelectedValue = .Item("ISSUE_ST")
                Name_cmb.SelectedValue = .Item("POLICY_CLIENT_CD")
            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_ISSUE_DTL_DAO
        Dim vo As New DAOMain.POLICY_ISSUE_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .REMARKS = Remarks_txt.Text
            .POLICY_ISSUE_DT = cFunc.ParseSetDate(IssueDate_txt.Text)
            .ISSUE_ST = IssueStatus_cmb.SelectedValue.ToString
            .POLICY_CLIENT_CD = ""
            .ISSUE_TYPE = ""

            Select Case IsNothing(ViewState("CID"))
                Case True
                    .POLICY_ISSUE_CD = cFunc.GetLastID("policy_issue_cd", "policy_issue_dtl")
                    .POLICY_ISSUE_CREA_BY = Session("UserName")
                    .POLICY_ISSUE_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
                Case False
                    .POLICY_ISSUE_CD = ViewState("CID")
                    .POLICY_ISSUE_LAST_UPD_BY = Session("UserName")
                    .POLICY_ISSUE_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        If Not IsNothing(ViewState("PageSource")) Then
            InitSessions()
            Response.Redirect("~/" & strPath & "/Search_CIssues.aspx")
        Else
            InitSessions()
            Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
        End If
    End Sub
End Class
